﻿using ETask.Api.Dtos;
using ETask.Api.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace ETask.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class ProjectController : ControllerBase
    {
        private readonly IProjectService _projectService;
        private readonly IAuditLogService _auditLogService;

        public ProjectController(IProjectService projectService, IAuditLogService auditLogService)
        {
            _projectService = projectService;
            _auditLogService = auditLogService;
        }

        #region 项目管理
        [HttpPost("addProject")]
        public async Task<IActionResult> AddProject([FromBody] CreateProjectDto projectDto)
        {
            try
            {
                var result = await _projectService.CreateAsync(projectDto);
                
                // 记录项目创建日志
                await _auditLogService.LogSuccessAsync("创建", "项目", $"创建项目 {projectDto.Name}", result?.Id, projectDto.Name);
                
                return Ok(ApiResponse.Ok(result, "添加成功"));
            }
            catch (Exception ex)
            {
                await _auditLogService.LogErrorAsync("创建", "项目", $"创建项目 {projectDto.Name} 失败", ex.Message);
                return Ok(ApiResponse.Fail(500, $"添加项目失败: {ex.Message}"));
            }
        }

        [HttpPost("updateProject")]
        public async Task<IActionResult> UpdateProject([FromBody] UpdateProjectDto updateProjectDto)
        {
            try
            {
                await _projectService.UpdateAsync(updateProjectDto.Id, updateProjectDto);
                
                // 记录项目更新日志
                await _auditLogService.LogSuccessAsync("更新", "项目", $"更新项目 {updateProjectDto.Name}", updateProjectDto.Id, updateProjectDto.Name);
                
                return Ok(ApiResponse.Ok("更新成功"));
            }
            catch (Exception ex)
            {
                await _auditLogService.LogErrorAsync("更新", "项目", $"更新项目 {updateProjectDto.Name} 失败", ex.Message, updateProjectDto.Id, updateProjectDto.Name);
                return Ok(ApiResponse.Fail(500, $"更新项目失败: {ex.Message}"));
            }
        }

        [HttpPost("delById")]
        public async Task<IActionResult> DelById([FromBody] PrimaryKey key)
        {
            try
            {
                // 先获取项目信息用于日志记录
                var project = await _projectService.GetByIdAsync(key.Id);
                var projectName = project?.Name ?? "未知项目";
                
                await _projectService.DeleteAsync(key.Id);
                
                // 记录项目删除日志
                await _auditLogService.LogSuccessAsync("删除", "项目", $"删除项目 {projectName}", key.Id, projectName);
                
                return Ok(ApiResponse.Ok("删除成功"));
            }
            catch (Exception ex)
            {
                await _auditLogService.LogErrorAsync("删除", "项目", $"删除项目失败", ex.Message, key.Id);
                return Ok(ApiResponse.Fail(500, $"删除项目失败: {ex.Message}"));
            }
        }

        [HttpPost("getList")]
        public async Task<IActionResult> GetList([FromBody] ProjectListParams listParams)
        {
            var res = await _projectService.GetList(listParams);
            return Ok(res);
        }

        [HttpPost("getInfo")]
        public async Task<IActionResult> GetInfo([FromBody]PrimaryKey key)
        {
            var res = await _projectService.GetByIdAsync(key.Id);
            return Ok(ApiResponse.Ok(res));
        }
        #endregion


    }
}
